/******************************************************************************************************************
* File:EventUtility.java
* Course: 17655
* Project: Assignment 3
* Copyright: Copyright (c) 2009 Carnegie Mellon University
* Versions:
*	1.0 February 2009 - Initial rewrite of original assignment 3 (ajl).
*
* Description: This class illustrates how to utilize the event manager and provides a few
* basic utilities that can help developers test and debug their systems.
*
* Parameters: None
*
* Internal Methods: None
*
******************************************************************************************************************/
package SystemB;

import java.util.LinkedHashMap;

import TermioPackage.*;
import EventPackage.*;
import InstrumentationPackage.CommandConsoleOption;

public class SysBSIMConsole
{
	static LinkedHashMap<String, CommandConsoleOption> optionMap;
	static int eventId = 100;
	
	private static void setOptions() {
		optionMap = new LinkedHashMap<String, CommandConsoleOption>();
		
		optionMap.put("1", new CommandConsoleOption("1","Simulate Window Break Emergency",eventId,"WB1"));
		optionMap.put("2", new CommandConsoleOption("2","Stop Simulated Window Break Emergency\n",eventId,"WB0"));
		optionMap.put("3", new CommandConsoleOption("3","Simulate Door Break Emergency",eventId,"DB1"));
		optionMap.put("4", new CommandConsoleOption("4","Stop Simulated Door Break Emergency\n",eventId,"DB0"));
		optionMap.put("5", new CommandConsoleOption("5","Simulate Motion Detection Emergency",eventId,"MD1"));
		optionMap.put("6", new CommandConsoleOption("6","Stop Simulated Motion Detection Emergency\n",eventId,"MD0"));
		optionMap.put("7", new CommandConsoleOption("5","Simulate Fire Emergency",eventId,"FA1"));
		optionMap.put("8", new CommandConsoleOption("6","Stop Simulated Fire Emergency\n",eventId,"FA0"));
	}
	
	public static void main(String args[])
	{
    	Termio UserInput = new Termio();// Termio IO Object
		String EvtMgrIP;				// Event Manager IP address
		boolean Done = false;			// Main loop flag
		String Option;					// Menu choice from user
		EventManagerInterface ef = null;// Interface object to the event manager

		/////////////////////////////////////////////////////////////////////////////////
		// Get the IP address of the event manager
		/////////////////////////////////////////////////////////////////////////////////

  		System.out.println( "\nEnter IP address of event manager or..." );
  		System.out.print( "press enter if on local machine: " );
		EvtMgrIP = UserInput.KeyboardReadString();

		System.out.print("\n\nAttempting to register..." );

		if (EvtMgrIP.length() == 0)
		{
				try
				{
					// Here we create an event manager interface object. This assumes
					// that the event manager is on the local machine

					ef = new EventManagerInterface();
				}

				catch (Exception e)
				{
					System.out.println("Error instantiating event manager interface: " + e);

				} // catch

		} else {

			if (EvtMgrIP.length() != 0)
			{
				try
				{
					// Here we create an event manager interface object. This assumes
					// that the event manager is NOT on the local machine

					ef = new EventManagerInterface( EvtMgrIP );
				}

				catch (Exception e)
				{
					System.out.println("Error instantiating event manager interface: " + e);

				} // catch

			}// if

		}// if

		// Here we check to see if registration worked. If ef is null then the
		// event manager interface was not properly created.

		if (ef != null )
		{
			System.out.println("Registered with the event manager.\n\n" );
			
			setOptions();

			System.out.println( "\n\nSecurity Simulation Command Console: \n" );
			
			while (!Done)
			{
				// Here is the main menu
				System.out.println( "Select an Option: \n" );
				
				for (String key : optionMap.keySet()) {
					System.out.println(key + ":" + optionMap.get(key).getDescription());
				}
				
			
				System.out.println( "X: Exit \n" );
				System.out.print( "\n>>>> " );
				
				Option = UserInput.KeyboardReadString();

				if (optionMap.containsKey(Option)) {
			    	try
			    	{	
			    		System.out.println(optionMap.get(Option).getEventId() + " " + optionMap.get(Option).getEventMessage());
			    		ef.SendEvent(new Event(optionMap.get(Option).getEventId(), optionMap.get(Option).getEventMessage()));			    		
			    	} // try

			    	catch (Exception e)
			    	{
						System.out.println("Error:: " + e);

			    	} // catch
				}
				
				//////////// option X ////////////

				if ( Option.equalsIgnoreCase( "X" ) )
				{
					// Here the user is done, so we set the Done flag and unregister
					// the event interface from the event manager. If you fail to
					// unregister, the event manager doesn't know to remove queues.
					// These become dead queues and they collect events and will eventually
					// cause problems for the event manager.

					Done = true;

					try
					{
						ef.UnRegister();

			    	} // try

			    	catch (Exception e)
			    	{
						System.out.println("Error unregistering: " + e);

			    	} // catch

				} // if

			} // while

		} else {

			System.out.println("Unable to register with the event manager.\n\n" );

		} // if

  	} // main

} // CommandConsole
